// pages/driver/addDriverPhoto/addDriverPhoto.js
const handler = require("../../../utils/function.js");
var app = getApp(),
  api = require("../../../api.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pageType: null,
    fontImage: null,
    backImage: null,
    idCardPhoto: null,
    idCardPhotoTest: false,
    driverPhoto: null,
    driverPhotoTest: false,
    positive: '', //驾照正面照片
    reverse: '', //驾照反面照片

    idpositive: '',
    idreverse: '',
    xszpositive: '',
    xszreverse: '',
    clzppositive: '',
    yyzgzpositive: '',
    czczppositive: '',
    jdkpositive: '',






    drivers: "",
  },


  /**微蚁科技 2020 08 03 重写整合图片上传功能**/
  wyupload(e) {
    if (this.data.isChange) {
      return
    }
    var that = this,
      type = e.currentTarget.dataset.type;
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        wx.showLoading({})
        wx.uploadFile({
          url: api.driver.uploadcar, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'access_token': wx.getStorageSync("access_token"),
            '_uniacid': app.siteInfo.uniacid,
            '_acid': app.siteInfo.acid,
          },
          success: function (res) {
            wx.hideLoading()
            if (res.statusCode == 200) {
              var jon = JSON.parse(res.data);
              if (jon.code == 0) {
                wx.showToast({
                  title: jon.msg,
                  icon: 'none'
                })
                return
              }
              if (type == 'positive') {
                that.setData({
                  positive: jon.data
                })
                wx.setStorageSync("positive", jon.data)
              } else if (type == 'reverse') {
                that.setData({
                  reverse: jon.data
                })
                wx.setStorageSync("reverse", jon.data)
              } else if (type == 'idpositive') {
                that.setData({
                  idpositive: jon.data
                })
                wx.setStorageSync("idpositive", jon.data)
              } else if (type == 'idreverse') {
                that.setData({
                  idreverse: jon.data
                })
                wx.setStorageSync("idreverse", jon.data)
              } else if (type == 'xszpositive') {
                that.setData({
                  xszpositive: jon.data
                })
                wx.setStorageSync("xszpositive", jon.data)
              } else if (type == 'xszreverse') {
                that.setData({
                  xszreverse: jon.data
                })
                wx.setStorageSync("xszreverse", jon.data)
              } else if (type == 'clzppositive') {
                that.setData({
                  clzppositive: jon.data
                })
                wx.setStorageSync("clzppositive", jon.data)
              } else if (type == 'yyzgzpositive') {
                that.setData({
                  yyzgzpositive: jon.data
                })
                wx.setStorageSync("yyzgzpositive", jon.data)
              } else if (type == 'jdkpositive') {
                that.setData({
                  jdkpositive: jon.data
                })
                wx.setStorageSync("jdkpositive", jon.data)
              } else if (type == 'czczppositive') {
                that.setData({
                  czczppositive: jon.data
                })
                wx.setStorageSync("czczppositive", jon.data)
              }
            } else {
              wx.showToast({
                title: res.msg,
                icon: "none"
              })
            }
          }
        })
      }
    })
  },
  /*author By weichat_admin  2538817020**/



















  // 图片正面上传
  // upFontloadFile: function (e) {
  //   if (this.data.isChange){
  //     return
  //   }
  //   var type = e.currentTarget.dataset.type;
  //   let that = this;
  //   wx.chooseImage({
  //     success: function (res) {
  //       var tempFilePaths = res.tempFilePaths
  //       wx.showLoading({})
  //       wx.uploadFile({
  //         url: api.driver.uploadcar, //仅为示例，非真实的接口地址
  //         filePath: tempFilePaths[0],
  //         name: 'file',
  //         formData: {
  //           'access_token': wx.getStorageSync("access_token"),
  //           '_uniacid': app.siteInfo.uniacid,
  //           '_acid': app.siteInfo.acid,
  //         },
  //         success: function (res) {
  //           wx.hideLoading()
  //           if (res.statusCode == 200) {
  //             var jon = JSON.parse(res.data);
  //             if (jon.code == 0) {
  //               wx.showToast({
  //                 title: jon.msg,
  //                 icon:'none'
  //               })
  //               return
  //             }
  //             that.setData({
  //               positive: jon.data
  //             })
  //             wx.setStorageSync("positive", jon.data)
  //           } else {
  //             /* wx.showToast({
  //                   title: res.msg,
  //                   icon: "none"
  //               })*/
  //           }
  //         }
  //       })
  //     }
  //   })
  // },
  // 图片反面上传
  // upBackloadFile: function (e) {
  //   if (this.data.isChange){
  //     return
  //   }
  //   const that = this;
  //   var type = e.currentTarget.dataset.type;
  //   wx.chooseImage({
  //     success: function (res) {
  //       var tempFilePaths = res.tempFilePaths
  //       /* that.setData({
  //            backImage: tempFilePaths
  //        });*/
  //       wx.showLoading({})
  //       wx.uploadFile({
  //         url: api.driver.uploadcar, //仅为示例，非真实的接口地址
  //         filePath: tempFilePaths[0],
  //         name: 'file',
  //         formData: {
  //           'user': 'test',
  //           'access_token': wx.getStorageSync("access_token"),
  //           '_uniacid': app.siteInfo.uniacid,
  //           '_acid': app.siteInfo.acid,
  //         },
  //         success: function (res) {
  //           wx.hideLoading()
  //           if (res.statusCode == 200) {
  //             var jon = JSON.parse(res.data);
  //             if (jon.code == 0) {
  //               wx.showToast({
  //                 title: jon.msg,
  //                 icon: 'none'
  //               })
  //               return
  //             }
  //               that.setData({
  //                 reverse: jon.data
  //               })
  //               wx.setStorageSync("reverse", jon.data)
  //             // }
  //           } else {
  //             /* wx.showToast({
  //                  title: res.msg,
  //                  icon: "none"
  //              })*/
  //           }
  //         }
  //       })
  //     }
  //   })
  // },
  // intoPageHandler 页面跳转
  intoPageHandler: function (e) {
    let that = this;
    if (this.data.isChange == 'true') {
      wx.showModal({
        title: '提示',
        content: '你已上传图片，请勿重新上传',
        success: function (res) {
          wx.navigateBack({
            url: e.currentTarget.dataset.skip + '?type=' + that.data.pageType + '&reverse=' + that.data.reverse + '&positive=' + that.data.positive + '&driver=' + that.data.driver,
            delta: 1
          })
        }
      })

    } else if (this.data.positive && this.data.reverse) {
      wx.showToast({
        title: '上传成功',
        success: function () {
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 1500)

        }
      })
    }


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 判断是否可以修改图片
    if (options.isChange == 'true') {
      this.setData({
        isChange: true
      })
    }
    /*        wx.setStorageSync("driverss",options.driver)*/
    if (options.type == 'driverPhoto') {
      if (wx.getStorageSync('driving_license') != '') {
        this.setData({
          positive: wx.getStorageSync('driving_license').driverfPhoto,
          reverse: wx.getStorageSync('driving_license').driverPhoto
        })
      }
      if (wx.getStorageSync('id_card_pic') != '') {
        this.setData({
          idpositive: wx.getStorageSync('id_card_pic').id_card_pic,
          idreverse: wx.getStorageSync('id_card_pic').id_card_fpic
        })
      }
      if (wx.getStorageSync('xszpositive') != '') {
        this.setData({
          xszpositive: wx.getStorageSync('xszpositive')
        })
      }
      if (wx.getStorageSync('xszreverse') != '') {
        this.setData({
          xszreverse: wx.getStorageSync('xszreverse')
        })
      }
      if (wx.getStorageSync('clzppositive') != '') {
        this.setData({
          clzppositive: wx.getStorageSync('clzppositive')
        })
      }
      if (wx.getStorageSync('czczppositive') != '') {
        this.setData({
          czczppositive: wx.getStorageSync('czczppositive')
        })
      }
      if (wx.getStorageSync('yyzgzppositive') != '') {
        this.setData({
          yyzgzppositive: wx.getStorageSync('yyzgzppositive')
        })
      }
      if (wx.getStorageSync('jdkpositive') != '') {
        this.setData({
          jdkpositive: wx.getStorageSync('jdkpositive')
        })
      }
    }
    // if (options.type == 'idCardPhoto') {
    //   if (wx.getStorageSync('id_card_pic') != '') {
    //     this.setData({
    //       positive: wx.getStorageSync('id_card_pic').id_card_pic,
    //       reverse: wx.getStorageSync('id_card_pic').id_card_fpic
    //     })
    //   }
    //   console.log('正面正面', this.data.reverse)
    //   console.log('正面正面22', this.data.pageType)

    // }
    // this.setData({
    //   pageType: options.type,
    //   isChange: options.isChange,
    // });
    // 获取优惠券
    // if (options.uid) {
    //   app.shareCouponsMessage(options.uid)
    // }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('onshow')
    var positive = wx.getStorageSync("positive") || '';
    var reverse = wx.getStorageSync("reverse") || '';
    var idpositive = wx.getStorageSync("idpositive") || '';
    var idreverse = wx.getStorageSync("idreverse") || '';
    var xszpositive = wx.getStorageSync("xszpositive") || '';
    var xszreverse = wx.getStorageSync("xszreverse") || '';
    var clzppositive = wx.getStorageSync("clzppositive") || '';
    var jdkpositive = wx.getStorageSync("jdkpositive") || '';
    var yyzgzpositive = wx.getStorageSync("yyzgzpositive") || '';
    var czczppositive = wx.getStorageSync("czczppositive") || '';
    console.log(positive, '-----------------')
    this.setData({
      driver: wx.getStorageSync("driver_information"),
      positive,idpositive,idreverse,xszpositive,xszreverse,jdkpositive,czczppositive,
      reverse,clzppositive,yyzgzpositive
    });
  },
  // 用户分享
  onShareAppMessage: function () {
    let title = app.shareAppMessage().title;
    let imageUrl = app.shareAppMessage().imageUrl;
    let desc = app.shareAppMessage().desc;
    let path = app.shareAppMessage().path;
    console.log(app.shareAppMessage())
    return {
      title: title,
      imageUrl: imageUrl,
      desc: desc,
      path: path,
    }
  },
})